package com.deer.wms.base.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.dao.ItemInfoMapper;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.Item.ItemInfoCriteria;
import com.deer.wms.base.system.model.Item.ItemInfoDto;
import com.deer.wms.base.system.model.Item.ItemType;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.base.system.service.ItemTypeService;
import com.deer.wms.busine.tactic.model.Batch.BatchTacticDetail;
import com.deer.wms.busine.tactic.model.QC.QcTacticDetail;
import com.deer.wms.busine.tactic.model.Upper.UpperTacticDetailDto;
import com.deer.wms.busine.tactic.service.*;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.system.manage.model.organization.Organization;
import com.deer.wms.system.manage.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 *
 * @author guo
 * @since 2019/11/19
 */
@Service
@Transactional
public class ItemInfoServiceImpl extends SuperServiceImpl<ItemInfoMapper, ItemInfo> implements ItemInfoService {

    @Autowired
    private ItemInfoMapper itemInfoMapper;

    @Autowired
    private BatchTacticService batchTacticService;

    @Autowired
    private BatchTacticDetailService batchTacticDetailService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private ItemTypeService itemTypeService;

    @Autowired
    private QcTacticDetailService qcTacticDetailService;

    @Autowired
    private UpperTacticService upperTacticService;

    @Autowired
    private UpperTacticDetailService upperTacticDetailService;

    @Autowired
    private ItemInfoService itemInfoService;

    @Override
    public List<ItemInfoDto> findList(ItemInfoCriteria criteria) {
        return itemInfoMapper.findList(criteria);
    }

    @Override
    public List<ItemInfo> findByName(String itemName) {
        return itemInfoMapper.findByName(itemName);
    }

    @Override
    public List<ItemInfo> findByCode(String itemCode) {
        return itemInfoMapper.findByCode(itemCode);
    }


    @Override
    public List<ItemInfoDto> itemInfoFindByItemTypeId(Integer itemTypeId) {
        return itemInfoMapper.itemInfoFindByItemTypeId(itemTypeId);
    }

    @Override
    public void deleteByCode(String itemCode) {
        itemInfoMapper.deleteByCode(itemCode);
    }

    @Override
    public String findBatchTacticCode(String itemCode) {


        ItemInfo itemInfo = this.findBy("item_code",itemCode);
        String batchTacticCode = itemInfo.getBatchTacticCode();
        List<BatchTacticDetail> list = batchTacticDetailService.findByBatchTacticCode(batchTacticCode);

        if(list.size()!=14){
            Integer itemTypeId = itemInfo.getItemTypeId();
            ItemType itemType = itemTypeService.getById(itemTypeId);
            if(itemType!=null){
                list = batchTacticDetailService.findByBatchTacticCode(batchTacticCode);
            }
            if(list.size()!=14){
                Integer organizationId = itemInfo.getOrganizationId();
                Organization organization = organizationService.getById(organizationId);
                batchTacticCode = organization.getBatchTacticCode();
                list = batchTacticDetailService.findByBatchTacticCode(batchTacticCode);
                if(list.size()!=14){
                    throw new ServiceException(CommonCode.BATCH_TACTIC_ERROR, "不存在该批次属性，请在物料或物料类型或货主中新建批次属性");
                }
            }
        }
        return batchTacticCode;
    }


    @Override
    public List<BatchTacticDetail> findBatchTacticDetails(String itemCode) {
        ItemInfo itemInfo = this.findBy("item_code",itemCode);
        String batchTacticCode = itemInfo.getBatchTacticCode();
        List<BatchTacticDetail> list = batchTacticDetailService.findByBatchTacticCode(batchTacticCode);

        if(list.size()!=14){
            Integer itemTypeId = itemInfo.getItemTypeId();
            ItemType itemType = itemTypeService.getById(itemTypeId);
            if(itemType!=null){
                list = batchTacticDetailService.findByBatchTacticCode(batchTacticCode);
            }
            if(list.size()!=14){
                Integer organizationId = itemInfo.getOrganizationId();
                Organization organization = organizationService.getById(organizationId);
                batchTacticCode = organization.getBatchTacticCode();
                list = batchTacticDetailService.findByBatchTacticCode(batchTacticCode);
                if(list.size()!=14){
                    throw new ServiceException(CommonCode.BATCH_TACTIC_ERROR, "不存在该批次属性，请在物料或物料类型或货主中新建批次属性");
                }
            }
        }
        return list ;
    }

    @Override
    public List<UpperTacticDetailDto> findUpperTacticDetails(String itemCode) {
        ItemInfo itemInfo = this.findBy("item_code",itemCode);
        String upperTacticCode = itemInfo.getUpperTacticCode();
        List<UpperTacticDetailDto> list = upperTacticDetailService.findByUpperTacticCode(upperTacticCode);

        if (ObjectUtil.isNull(list) || list.size() == 0 ) {
            Integer itemTypeId = itemInfo.getItemTypeId();
            ItemType itemType = itemTypeService.getById(itemTypeId);
            if(ObjectUtil.isNotNull(itemType)){
                String upperTacticCode2 = itemType.getUpperTacticCode();
                list = upperTacticDetailService.findByUpperTacticCode(upperTacticCode2);
            }
        }
        return list;
    }


    @Override
    public List<QcTacticDetail> findQcTacticDetails(String itemCode) {
        ItemInfo itemInfo = this.findBy("item_code",itemCode);
        String qcTacticCode = itemInfo.getQcTacticCode();
        List<QcTacticDetail> list = qcTacticDetailService.findByQcTacticCode(qcTacticCode);

        if(list==null){
            Integer itemTypeId = itemInfo.getItemTypeId();
            ItemType itemType = itemTypeService.getById(itemTypeId);
            if(itemType==null){
                list = qcTacticDetailService.findByQcTacticCode(qcTacticCode);
            }
            if(list==null){
                Integer organizationId = itemInfo.getOrganizationId();
                Organization organization = organizationService.getById(organizationId);
                qcTacticCode = organization.getQcTacticCode();
                list = qcTacticDetailService.findByQcTacticCode(qcTacticCode);
                if(list==null){
                    throw new ServiceException(CommonCode.QC_TACTIC_ERROR);
                }
            }
        }
        return list ;
    }


    @Override
    public void modifyItemState(String itemCode, String sate, CurrentUser currentUser) {
        ItemInfo itemInfo = itemInfoService.findBy("item_code",itemCode);
        itemInfo.setItemStatus(sate);
        super.updateById(itemInfo);
    }

    @Override
    public ItemInfo findBy(String field, String value) {
        QueryWrapper<ItemInfo> qw = new QueryWrapper<>();
        return this.getOne(qw.eq(field, value));
    }

    @Override
    public long findCount(ItemInfoCriteria criteria) {
        return baseMapper.findCount(criteria);
    }

}
